package ru.ipo.dces.plugins.admin.resultstable;

import ru.ipo.dces.utils.ResultUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: ilya
 * Date: 30.03.11
 * Time: 16:26
 */
public class MapTableCellRenderer extends DefaultTableCellRenderer {

    private String emptyText;
    private boolean substituteRenderer = false;

    public MapTableCellRenderer() {
        emptyText = "";
    }

    public String getEmptyText() {
        return emptyText;
    }

    public void setEmptyText(String emptyText) {
        this.emptyText = emptyText;
    }

    public boolean isSubstituteRenderer() {
        return substituteRenderer;
    }

    public void setSubstituteRenderer(boolean substituteRenderer) {
        this.substituteRenderer = substituteRenderer;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

        if (value == null) {
            value = emptyText;
        } else {

            @SuppressWarnings({"unchecked"})
            HashMap<String, String> array = (HashMap<String, String>) value;

            if (substituteRenderer) {
                KeySet keySet = KeySetsRegistry.getInstance().getKeySet(array.keySet());

                if (keySet != null && keySet.getRenderer() != null)
                    return keySet.getRenderer().getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            }

            value = ResultUtils.map2string(array);
        }

        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }

}
